package leetcode;

public class SumOfTwoInteger {

	public static void main(String[] args) {
		SumOfTwoInteger sum = new SumOfTwoInteger();
		sum.getSum(-1, -1);
	}
	public int getSum(int a, int b) {
		// just kidding
		// return a + b;
		int curBit = 0;
		int curCarry = 0;
		int sum = 0;
		int move = 0;
		while (a != 0 || b != 0) {
			if (curCarry == 0) {
				curBit = (a & 1) ^ (b & 1) ^ curCarry;
				curCarry = (a & 1) & (b & 1);
			} else {
				curBit = (a & 1) ^ (b & 1) ^ curCarry;
				curCarry = (a & 1) | (b & 1);
			}
			System.out.println("第" + (move + 1) + "位：" + curBit);
			sum = (curBit << move) ^ sum;
			move++;
			a >>>= 1;
			b >>>= 1;
		}
		System.out.println("move:" + move);
		//注意，move是从0开始的
		if(move < 32){
			if(curCarry != 0){
				sum = (curCarry << move) ^ sum;
			}
		}
		System.out.println("Sum:" + sum);
		return sum;
	}
}
